package com.sc.javaee.controller;

import com.sc.javaee.pojo.Address;
import com.sc.javaee.pojo.Result;
import com.sc.javaee.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@CrossOrigin
@RestController
public class AddressController {
    @Autowired
    AddressService as;
    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address a){
        Address savedAddress =as.add(a) ;
        if (savedAddress != null && savedAddress.getAddressId() != null) {
            return new Result(1, "新增地址成功", savedAddress);
        }
        return new Result(0, "新增地址失败");
    }
    @RequestMapping("/delAddress")
    public Result delAddress(Integer id){
        int i=as.del(id);
        if (i>0) return new Result(1,"删除地址成功");
        return new Result(0,"删除地址失败");
    }
    //动态更新
    @RequestMapping("/updateAddress")
    public Result updateAddress(@RequestBody Address a){
        int i=as.update(a);
        if(i>0) return new Result(1,"更新地址成功",a);
        return new Result(0,"更新地址失败");
    }
    //取消所有默认 更新addressStatus为0
    @RequestMapping("/updateStatus")
    public Result updateStatus(){
        int i=as.updateStatus();
        if (i>0) return new Result(1,"取消所有默认成功");
        return new Result(0,"取消所有默认失败");
    }
    //查询所有地址
    @RequestMapping("/selectAddress")
    public Result selectAddress(){
        List<Address> list=as.select();
        return new Result(1,"查询地址成功",list);
    }
    //回显地址
    @RequestMapping("/selectByAddressId")
    public Result selectByAddressId(Integer id){
        Address a=as.selectByAddressId(id);
        if (a!=null) return new Result(1,"查询单个地址成功",a);
        return new Result(0,"没有该地址");
    }
    //查询默认地址
    @RequestMapping("/getDefaultAddress")
    public Result getDefaultAddress(){
        Address a = as.getDefaultAddress();
        if(a!= null) {
            return new Result(1,"查询默认地址成功", a);
        }
        return new Result(0,"暂无默认地址");
    }
}
